/**
 * @autor Flower
 * @descrption 编辑会员信息
 */

import HttpService from '../../../../services/HttpService'
import ApiUrl from '../../../../services/ApiUrl'
import EditType from '../edit-types'
import Util from '../../../../services/Util'

Page({
  data: {
    date:'',
    isEdit: true,
    isSelf: true,
    avatar: '',
    name: '',
    id: ''
  },
  //提交表单注册／登录
  formSubmit: function(e){
      console.log( 'e', e.detail );
      let self = this;
      if( !e.detail.value.name || !e.detail.value.date ) return;
      let opts = {
          apiObj: ApiUrl.editMember,
          path: {
                member_id: self.data.id
          },
          body:{
              name: e.detail.value.name,
              birthday: Util.dateToUnix( e.detail.value.date ) 
          }
      }
      HttpService.accessAPI(opts).then(function(result){
          console.log( 'edit success', result );
          if( self.data.isSelf ){
            let profile = wx.getStorageSync('profile');
            profile.member.name = result.data.name;
            profile.member.birthday = Util.unixToDate( result.data.birthday );
            profile.member.mobile = result.data.mobile;
            wx.setStorageSync('profile', profile);
          }else{
            let members = wx.getStorageSync('members') || [];
            for( let i = 0; i<members.length;i++ ){
                if( members[i].id == result.data.id ){
                    members[i].name = result.data.name;
                    members[i].birthday = Util.unixToDate( result.data.birthday );
                    members[i].mobile = result.data.mobile;
                }
            }
            wx.setStorageSync('members', members);
          }
          wx.navigateBack();
      });
  },
  bindDateChange: function(e){
      let self = this;
      self.setData({
        date: e.detail.value
      })
  },
  logOut:function () {
        let self = this;

        let opts = {
            apiObj:ApiUrl.logOut,
            body:{entry:'wechat'}
        };
        wx.showModal({
            content:'确定登出用户？',
            success: function(res) {
                if (res.confirm) {
                    console.log('用户点击确定');
                    HttpService.accessAPI(opts).then(function(result){
                        wx.navigateTo({ url: "/pages/auth/login/login"});
                    });
                } else if (res.cancel) {

                }
            }

        });

    },
  onLoad: function (option) {
      console.log( 'this edit', option );

      let self = this;
      if( option.type === EditType.profile.completeInfo ){
          self.setData({
              isEdit: false,
              isSelf: true,
          })
      }
      if( option.type === EditType.profile.editInfo ){
          self.setData({
              isEdit: true,
              isSelf: true,
              date: option.birthday,
              name: option.name,
              avatar: option.avatar,
              id: option.id
          })
      }
      if( option.type === EditType.member.editInfo ){
          self.setData({
              isEdit: true,
              isSelf: false,
              date: option.birthday,
              name: option.name,
              avatar: option.avatar || '/images/avatar-placeholder.jpeg',
              id: option.id
          })
      }
    console.log('onLoad')
  }
})